<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of group
 *
 * @author Admin
 */
class group extends MY_Controller{
	
	function __construct() {
		parent::__construct();
		$this->load->model('model_group');
	}
	public function index()
	{
		$this->data['temp'] = 'admin/group/index';
		$this->load->view('admin/main',$this->data);
	}
	
	public function read()
	{
		// Khoi tao phan trang
		$pagination = pagination_init($this->model_group->get_total(), request('p',1));
		
		//Doc du lieu
		$groups = $this->model_group->get_list(array(
			'limit'	=> array($pagination->from,$pagination->limit),
		));
		echo json_encode(array(
			'state'	=> 'success',
			'data'	=> $groups,
			'pagination'	=> $pagination
		));
	}
	
	public function detail(){
		$id = request('id');
		$data = $this->model_group->get_info($id);
		$data->permissions = json_decode($data->permissions);
		$permissions = array();
		if(is_array($data->permissions)){
			$this->load->model('model_permission');
			foreach($data->permissions as $permission)
			{
				$permission = $this->model_permission->get_info_rule(array(
					'permission' => $permission
				),'id');
				if($permission!==false) $permissions[]=$permission->id;
			}
		}
		$data->permissions = $permissions;
		echo json_encode(array(
			'state'	=> 'success',
			'data'	=> $data,
		));
	}
	
	public function create()
	{
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		// Rule
		$this->form_validation->set_rules('name','lang:group_name','required');
		
		// Lay data tu POST
		$data = request_filter(array('name','description','permissions'));
		
		if($this->form_validation->run())
		{
			if(is_array($data['permissions']))
			{
				$this->load->model('model_permission');
				$permissions=array();
				foreach($data['permissions'] as $permission_id)
				{
					$permission = $this->model_permission->get_info($permission_id);
					$permissions[] = $permission->permission;
				}
				$data['permissions'] = json_encode($permissions);
			}
			else
				unset($data['permissions']);
			$this->model_group->create($data);
			echo json_encode(array(
				'state'	=> 'success',
				'title'	=> lang('notice'),
				'message'	=> lang('group_create_success')
			));
		}else{
			echo json_encode(array(
				'state'	=> 'error',
				'title'	=> lang('error'),
				'message'	=> form_error_message($data)
			));
		}
	}
	public function update(){
		$this->load->helper('form');
		$this->load->library('form_validation');
		
		// Rule
		$this->form_validation->set_rules('name','lang:group_name','required');
		
		// Lay data tu POST
		$data = request_filter(array('name','description','permissions'));
		$id = request('id');
		
		if($this->form_validation->run())
		{
			if(is_array($data['permissions']))
			{
				$this->load->model('model_permission');
				$permissions=array();
				foreach($data['permissions'] as $permission_id)
				{
					$permission = $this->model_permission->get_info($permission_id);
					$permissions[] = $permission->permission;
				}
				$data['permissions'] = json_encode($permissions);
				
			}
			else
				unset($data['permissions']);
			$this->model_group->update($id,$data);
			echo json_encode(array(
				'state'	=> 'success',
				'title'	=> lang('notice'),
				'message'	=> lang('group_update_success')
			));
		}else{
			echo json_encode(array(
				'state'	=> 'error',
				'title'	=> lang('error'),
				'message'	=> form_error_message($data)
			));
		}
	}
	public function delete(){
		$id = request('id');
		$this->model_group->del($id);
		echo json_encode(array(
			'state'	=> 'success',
			'id'	=> $id,
		));
	}
	
	public function get_all()
	{
		echo json_encode(array(
			'state'	=> 'success',
			'data'	=> $this->model_group->get_list(array(
				'select'	=> 'id,name',
			))
		));
	}
}

?>
